/**
 * Created by wenwu on 02/05/2017.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');

    //if(utilities.getParameterByName('openId')===''){
    //    location.href = 'http://friend.jxyouth.org.cn/wxOAuth?appId=wxe9a08de52d2723ba&callBackUrl=index.html';
    //}

	var app = new Vue({
		el: '#phone_bind',
		data: {
			smsSeconds: 60,
			smsAvailable: true,
			formObj: {
				phone: '',
				code: ''
			}
		},
		methods: {
			init: function () {
			},
			sendCaptcha: function () {
				if(app.smsAvailable){
					app.smsSeconds = 60;
					if (app.formObj.phone.trim() === '') {
						alert('请输入手机号！');
					} else {
						_(Config.SERVICE.USERCENTER.SEND_CAPTCHA, {phone: app.formObj.phone}, function (data) {
							console.log(data);
							app.smsAvailable = false;
							var timer = setInterval(function () {
								if (app.smsSeconds == 1) {
									app.smsAvailable = true;
									clearInterval(timer);
								}
								app.smsSeconds--;
							}, 1000);
						});
					}
				}
			},
			submit: function () {
				//if(utilities.getParameterByName('openId')===''){
                //    location.href = 'http://friend.jxyouth.org.cn/wxOAuth?appId=wxe9a08de52d2723ba&callBackUrl=index.html';
				//}
				if (app.formObj.phone.trim() === '') {
					alert('请输入手机号！');
				} else if (app.formObj.code.trim() === '') {
					alert('请输入验证码！');
				} else {
					_(Config.SERVICE.USERCENTER.PHONE_BIND, {
						phone: app.formObj.phone,
						code: app.formObj.code,
						openId: utilities.getParameterByName('openId')
					}, function (data) {
						console.log(data);
						if (data.type === 'SUCCESS') {
							alert('绑定成功！');
							sessionStorage.setItem('id', data.obj.id);
							location.href = 'index.html';
						} else {
							alert(data.errmsg);
						}
					});
				}
			}
		}
	});
	app.init();
});